﻿#include "redis_task.h"
#include "redis_manager.h"
#include "redis_data.h"
#include "redis_db_connection.h"
#include "log.h"

CRedisDbTask::CRedisDbTask(const std::string &instName, const std::string &dbName, const std::string &collectionName, unsigned type, const std::string &queryString, const std::string &docString)
	: m_instName(instName)
	, m_dbName(dbName)
	, m_collectionName(collectionName)
	, m_type(type)
	, m_queryString(queryString)
	, m_bFirst(true)
{
	m_taskInstName = "RedisDbTask_" + m_instName;
	m_idString = CRedisDbData::GetIdString(m_instName, m_dbName, m_collectionName);
}

CRedisDbTask::~CRedisDbTask()
{

}

std::shared_ptr<MultiTask::TaskBase> CRedisDbTask::SetCB(const CBType & cb)
{
	m_cb = cb;
	return shared_from_this();
}

void CRedisDbTask::Process(void *sharedObj)
{
	if (sharedObj == nullptr)
	{
		LOG_ERROR_TO("CRedisDbTask", Fmt("task process failure, no find redisdb obj"));
		return;
	}
	CRedisDbConnection *client = (CRedisDbConnection *)sharedObj;
	bool createRet = false; /* Todo: = client->Connect(m_dbName, m_collectionName);*/
	if (!createRet)
	{
		return;
	}

	if (m_datas)
	{
		auto &datas = *m_datas.get();
		for (size_t i = 0; i < datas.size(); i++)
		{
			switch (datas[i].type)
			{
			}
			
		}
	}
	// redisCommand
}

void CRedisDbTask::OnCompleted()
{
	CRedisDbData::get_mutable_instance().SetDataState(m_idString, e_opt_state_idle);
	auto task = std::make_shared<CRedisDbTask>(m_instName, m_dbName, m_collectionName, 0, "", "");
	task->m_bFirst = false;
	if (m_cb)	//for test
	{
		task->m_cb = m_cb;
		LOG_INFO_TO("CRedisDbTask: ", " insert some record completed ...");
	}
	task->Go();
}

//void * CRedisDbTask::CreateSharedObj(unsigned index)
//{
//	auto &objs = CRedisDbManager::get_mutable_instance().GetDbInstance(m_instName + "_bulk");
//	if (index >= objs.size())
//	{
//		return nullptr;
//	}
//	return objs[index].get();
//}

void CRedisDbTask::Go()
{
	if (m_bFirst)
	{
		m_bFirst = false;
		switch (m_type)
		{
		
		}
	}

	
}

